package com.bawei.common.widget

import android.content.Context
import android.graphics.Bitmap
import android.util.AttributeSet
import android.util.Log
import android.view.KeyEvent
import com.tencent.smtt.export.external.interfaces.ConsoleMessage
import com.tencent.smtt.export.external.interfaces.JsPromptResult
import com.tencent.smtt.export.external.interfaces.JsResult
import com.tencent.smtt.sdk.WebChromeClient
import com.tencent.smtt.sdk.WebSettings
import com.tencent.smtt.sdk.WebView
import com.tencent.smtt.sdk.WebViewClient

/**
 * Author: Jason Zhang
 * Date: 2024/8/24
 * Desc: 自定义X5浏览器组件
 */
class X5WebView constructor(context: Context, attrs: AttributeSet) : WebView(context, attrs){


    init {
        //设置客户端
        this.webViewClient = X5WebViewClient()
        this.webChromeClient = X5WebChromeClient()
        //初始化参数
        initX5WebView()
    }


    private fun initX5WebView(){
        var webSetting = this.settings
        webSetting.javaScriptEnabled = true //是否支持JS脚本
        webSetting.allowFileAccess = true //是否支持文件访问
        webSetting.loadsImagesAutomatically = true //是否支持自动加载图片
        webSetting.javaScriptCanOpenWindowsAutomatically = true //是否支持JS弹窗
        webSetting.layoutAlgorithm = WebSettings.LayoutAlgorithm.NARROW_COLUMNS
        webSetting.setSupportMultipleWindows(false) //是否支持多窗口
        webSetting.useWideViewPort = true // 是否支持小窗口
        webSetting.setSupportZoom(false) //是否支持页面缩放
        webSetting.builtInZoomControls = false //是否支持放大缩小控制按钮
        webSetting.setAppCacheEnabled(true) //是否支持缓存
        webSetting.cacheMode = WebSettings.LOAD_NO_CACHE //缓存模式
        webSetting.domStorageEnabled = true //是否支持Dom存储
        webSetting.setGeolocationEnabled(false) //是否支持定位
    }

    /**
     * 控制H5页面加载
     */
    inner class X5WebViewClient: WebViewClient(){

        //H5页面加载完成
        override fun onPageStarted(webView: WebView?, url: String?, p2: Bitmap?) {
            super.onPageStarted(webView, url, p2)
        }

        //H5页面关闭
        override fun onPageFinished(webView: WebView?, url: String?) {
            super.onPageFinished(webView, url)
        }

        //拦截H5页面中按键点击事件
        override fun shouldOverrideKeyEvent(p0: WebView?, p1: KeyEvent?): Boolean {
            return super.shouldOverrideKeyEvent(p0, p1)
        }

        //拦截H5页面中URL加载
        override fun shouldOverrideUrlLoading(webView: WebView?, url: String?): Boolean {
            webView?.loadUrl(url) //可以替换URL地址
            return true
        }
    }

    /**
     * 控制H5页面中所有用户交互
     */
    inner class X5WebChromeClient: WebChromeClient(){

        override fun onJsAlert(p0: WebView?, p1: String?, p2: String?, p3: JsResult?): Boolean {
            return true
        }

        override fun onJsConfirm(p0: WebView?, p1: String?, p2: String?, p3: JsResult?): Boolean {
            return true
        }

        override fun onJsPrompt(p0: WebView?, p1: String?, p2: String?, p3: String?, p4: JsPromptResult?): Boolean {
            return true
        }

        override fun onJsBeforeUnload(p0: WebView?, p1: String?, p2: String?, p3: JsResult?): Boolean {
            return true
        }

        override fun onConsoleMessage(p0: ConsoleMessage?): Boolean {
            Log.i("X5WebChromeClient", "onConsoleMessage: ${p0?.message()}")
            return true
        }

    }

}